package com.kaifamiao.www.demo.do0921;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;

public class FileInputStreamDome2 {
    public static void main(String[] args) {

        File file = new File("E:\\kaifamiao\\workspace\\idea--space\\io\\file-input.txt");

        if (file.exists()&& file.isFile()){
            try ( FileInputStream fis = new FileInputStream(file);) {

                byte[] bytes = new byte[2];
//                while (true){
//                    int n;//声明变量，用于统计实际读取到的字节数
//                    n = fis.read(bytes);//一次读取多个字节
//                    if (n == -1){
//                        break;
//                    }
//                    Charset gbk = Charset.forName("GBK");
//                    String s = new String(bytes,gbk);
//                    //IDEA程序运行时的编码是UTF-8，而file-input.txt采用GBK编码
//                    System.out.print(s);
//                }
                int c;
                Charset GBK = Charset.forName("gbk");
                while ((c = fis.read(bytes)) != -1){

                    String s = new String(bytes,GBK);
                    System.out.print(s);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }


        }

    }
}
